package com.thermal.thermal_inspection.service.impl;

import com.thermal.thermal_inspection.entity.*;
import com.thermal.thermal_inspection.mapper.*;
import com.thermal.thermal_inspection.service.InspectionSearchService;
import com.thermal.thermal_inspection.service.InspectionService;
import com.thermal.thermal_inspection.vo.InspectionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.*;

@Service
public class InspectionSearchServiceImpl implements InspectionSearchService {
    @Autowired
    private InspectionDetailMapper inspectionDetailMapper;
    @Autowired
    private InspectionWorkMapper inspectionWorkMapper;
    @Autowired
    private InspectionDataMapper inspectionDataMapper;
    @Autowired
    private MapMapper mapMapper;
    @Autowired
    private InspectionMapper inspectionMapper;
    @Autowired
    private BatchMapper batchMapper;
    /**
     *通过时间，用户id，地图id查询临时任务inspectionVOList
     */
    @Override
    public List<InspectionVO> searchNoBatchInspectionVO(Timestamp startTime, Timestamp endTime, int searchUserId, int mapId) {
        List<InspectionVO> inspectionVOList = new ArrayList<>();

        Date startDate = new Date(startTime.getTime());
        Date endDate = new Date(endTime.getTime());

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        calendar.add(Calendar.HOUR_OF_DAY, -8);  // 减去 8 个小时
        Date adjustedStartDate = calendar.getTime();  // 获取调整后的时间

        calendar.setTime(endDate);
        calendar.add(Calendar.HOUR_OF_DAY, -8);  // 减去 8 个小时
        Date adjustedEndDate = calendar.getTime();  // 获取调整后的时间

        //先通过Batch来查询临时任务有哪些
        List<Batch> batches = batchMapper.getBatchOfNoBatch();

        //这里要分四种情况，searchUserId,mapId都有可能为-1
        if(searchUserId != -1 && mapId != -1) {
            //从临时任务Batch里面取出workBeginId和workEndId来查询
            for (Batch batch : batches){
                List<InspectionWork> inspectionWorkList = inspectionWorkMapper.getInspectionWorkByTimeAndIdRange(batch.getWorkBeginId(),batch.getWorkEndId(),adjustedStartDate, adjustedEndDate);
                for (InspectionWork inspectionWork : inspectionWorkList){
                    List<InspectionDetail> inspectionDetailList = inspectionDetailMapper.getDetailByWorkIdAndUser(inspectionWork.getInspectionWorkId(),searchUserId);
                    //判断搜索出来的inspectionWork中是否有搜索的用户的巡检点位任务
                    if (!inspectionDetailList.isEmpty()){
                        //设置关于这个用户的工作总数以及完成数量
                        inspectionWork.setInspectionWorkNumber(inspectionDetailList.size());
                        inspectionWork.setInspectionWorkFinish(countFinishNumber(inspectionDetailList));
                        Inspection inspection = inspectionMapper.getInspectionById(inspectionWork.getInspectionId());
                        //判断查询到的地图ID是否为我们所需要的
                        if (inspection.getInspectionMapId() == mapId){
                            InspectionVO inspectionVO = new InspectionVO(mapMapper.getMap(mapId),inspection,inspectionWork);
                            inspectionVOList.add(inspectionVO);
                        }
                    }
                }
            }
            return inspectionVOList;
        }else if (searchUserId == -1 && mapId != -1){
            //从临时任务Batch里面取出workBeginId和workEndId来查询
            for (Batch batch : batches){
                List<InspectionWork> inspectionWorkList = inspectionWorkMapper.getInspectionWorkByTimeAndIdRange(batch.getWorkBeginId(),batch.getWorkEndId(),adjustedStartDate, adjustedEndDate);
                for (InspectionWork inspectionWork : inspectionWorkList){
                    Inspection inspection = inspectionMapper.getInspectionById(inspectionWork.getInspectionId());
                    //判断查询到的地图ID是否为我们所需要的
                    if (inspection.getInspectionMapId() == mapId){
                        InspectionVO inspectionVO = new InspectionVO(mapMapper.getMap(mapId),inspection,inspectionWork);
                        inspectionVOList.add(inspectionVO);
                    }
                }
            }
            return inspectionVOList;
        }else if (searchUserId != -1 && mapId == -1){
            //从临时任务Batch里面取出workBeginId和workEndId来查询
            for (Batch batch : batches){
                List<InspectionWork> inspectionWorkList = inspectionWorkMapper.getInspectionWorkByTimeAndIdRange(batch.getWorkBeginId(),batch.getWorkEndId(),adjustedStartDate, adjustedEndDate);
                for (InspectionWork inspectionWork : inspectionWorkList){
                    System.out.println(searchUserId);
                    List<InspectionDetail> inspectionDetailList = inspectionDetailMapper.getDetailByWorkIdAndUser(inspectionWork.getInspectionWorkId(),searchUserId);
                    //判断搜索出来的inspectionWork中是否有搜索的用户的巡检点位任务
                    if (!inspectionDetailList.isEmpty()){
                        //设置关于这个用户的工作总数以及完成数量
                        inspectionWork.setInspectionWorkNumber(inspectionDetailList.size());
                        inspectionWork.setInspectionWorkFinish(countFinishNumber(inspectionDetailList));
                        Inspection inspection = inspectionMapper.getInspectionById(inspectionWork.getInspectionId());
                        InspectionVO inspectionVO = new InspectionVO(mapMapper.getMap(inspection.getInspectionMapId()), inspection, inspectionWork);
                        inspectionVOList.add(inspectionVO);
                    }
                }
            }
            return inspectionVOList;
        }else {
            //从临时任务Batch里面取出workBeginId和workEndId来查询
            for (Batch batch : batches){
                System.out.println("adjustedStartDate"+adjustedStartDate);
                System.out.println("adjustedEndDate"+adjustedEndDate);
                List<InspectionWork> inspectionWorkList = inspectionWorkMapper.getInspectionWorkByTimeAndIdRange(batch.getWorkBeginId(),batch.getWorkEndId(),adjustedStartDate, adjustedEndDate);
                for (InspectionWork inspectionWork : inspectionWorkList){
                    Inspection inspection = inspectionMapper.getInspectionById(inspectionWork.getInspectionId());
                    InspectionVO inspectionVO = new InspectionVO(mapMapper.getMap(inspection.getInspectionMapId()), inspection, inspectionWork);
                    inspectionVOList.add(inspectionVO);
                }
            }
            return inspectionVOList;
        }
    }
    public static Integer countFinishNumber(List<InspectionDetail> inspectionDetails){
        int count = 0;
        for (InspectionDetail inspectionDetail : inspectionDetails){
            if (inspectionDetail.getInspectionDetailStatus() == 2 || inspectionDetail.getInspectionDetailStatus() == 4)
                count++;
        }
        return count;
    }
}
